//base.repository.ts
import { toTuofeng } from "../utils";
import DBPool from "../config/dbPool";
import { dbconfig } from "../config/db.config";
import IBaseRepository from "./interface.base.repository";
import SearchDto from "../dtos/search.dto";

export default class BaseRepository<V> implements IBaseRepository<V> {
  protected db: DBPool;
  protected tableName: string;

  constructor(tableName: string) {
    this.db = new DBPool(dbconfig);
    this.tableName = tableName
  }

  getList<T extends SearchDto>(dto:T): Promise<V[]> {
    return new Promise((resolve) => {
      this.db.query(`select * from ${this.tableName}`, [], (res) => {
        const jsonStr = JSON.stringify(res.results)
        const str = toTuofeng(jsonStr)
        const data = JSON.parse(str)
        console.log('Repository-getList', data);
        resolve(data)
      })
    })
  }
  add<E>(entity: E): void {
    const keys: string[] = []
    const values: string[] = []
    for (const key in entity) {
      if (Object.prototype.hasOwnProperty.call(entity, key)) {
        const element = entity[key];
        keys.push(`${key}=?`)
        values.push(element.toString())
      }
    }
    keys.join(',');
    const sql = `insert into  ${this.tableName} values(${keys})`
    console.log('sql', sql);

    return;
    this.db.query(``, values, (res) => {
      // const jsonStr = JSON.stringify(res.results)
      // const str = toTuofeng(jsonStr)
      // const data = JSON.parse(str)
      // console.log('Repository-getList', data);
      // resolve(data)
    })
  }
}